package com.zeny.express.module.mdm.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;

import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zeny.express.data.mdm.dto.parameter.UserSearchParameterDto;
import com.zeny.express.data.mdm.entity.UserEntity;
import com.zeny.express.framework.core.database.masterslave.MasterSlaveContext;
import com.zeny.express.framework.core.domain.Page;
import com.zeny.express.framework.core.service.BaseService;
import com.zeny.express.framework.shared.util.UUIDUtils;
import com.zeny.express.module.mdm.user.dao.UserEntityDao;
import com.zeny.express.module.mdm.user.service.UserService;

/**
 * 用户服务实现
 * 
 * @author zhuwei
 * @date 2013年8月15日 上午11:49:39
 */
@Service
public class UserServiceImpl extends BaseService implements UserService {

	@Autowired
	private UserEntityDao userEntityDao;

	@Autowired
	private MyBatisBatchItemWriter<UserEntity> batchInsertUsers;

	@Autowired
	private MyBatisBatchItemWriter<UserEntity> batchDeleteUsers;
	
	@PostConstruct
	public void afterPropertiesSet() {
		batchInsertUsers.setStatementId("batchInsertUsers");
		batchDeleteUsers.setStatementId("batchDeleteUsers");
	}

	/**
	 * 通过登录名查询用户
	 * 
	 * @author zhuwei
	 * @date 2013年8月15日 上午11:50:59
	 * @see com.zeny.express.api.mdm.user.IUserService#getUserByLoginCode(java.lang.String)
	 */
	@Override
	public UserEntity getUserByLoginCode(String loginCode) {

		LOGGER.debug(loginCode);

		return userEntityDao.getUserByLoginCode(loginCode);
	}

	/**
	 * 通过 ID查询用户
	 * 
	 * @author zhuwei
	 * @date 2013年8月15日 上午11:50:59
	 * @see com.zeny.express.module.mdm.user.service.UserService#getUserById(java.lang.String)
	 */
	@Override
	public UserEntity getUserById(String id) {

		LOGGER.debug(id);

		MasterSlaveContext.setSlave("mdmdb");

		return userEntityDao.getUserById(id);
	}

	/**
	 * 新增用户
	 * 
	 * @author zhuwei
	 * @date 2013年8月15日 上午11:50:59
	 * @see com.zeny.express.module.mdm.user.service.UserService#insertUser(com.zeny.express.data.mdm.entity.UserEntity)
	 */
	@Override
	public String insertUser(UserEntity entity) {

		LOGGER.debug(entity.toString());
		
		entity.setId(UUIDUtils.getTimeUUID());
		Date now = new Date();
		entity.setCreateTime(now);
		entity.setModifyTime(now);
		entity.setVersionNo(now.getTime());
		entity.setPassword("password");
		
		int i = userEntityDao.insertUser(entity);

		if (i != 1) {
			throw new RuntimeException("数据库插入错误！");
		}
		
		return entity.getId();
	}

	@Override
	public void updateUser(UserEntity entity) {

		LOGGER.debug(entity.toString());
		
		Date now = new Date();
		entity.setModifyTime(now);
		entity.setVersionNo(now.getTime());
		
		int i = userEntityDao.updateUser(entity);
		
		if (i != 1) {
			throw new RuntimeException("数据库更新错误！");
		}
		
	}

	/**
	 * 批量插入用户
	 * 
	 * @author zhuwei
	 * @date 2013年8月17日 下午2:05:28
	 * @see com.zeny.express.module.mdm.user.service.UserService#batchInsertUsers(java.util.List)
	 */
	@Override
	public void batchInsertUsers(List<UserEntity> list) {

		if (list == null || list.size() == 0) {
			return;
		}

		LOGGER.debug("batch " + String.valueOf(list.size()));

		batchInsertUsers.write(list);

		LOGGER.info(messageSource.getMessage("error.test", null, Locale.CHINESE));

	}

	@Override
	public void batchDeleteUsers(List<UserEntity> list) {
		
		if (list == null || list.size() == 0) {
			return;
		}

		LOGGER.debug("batch " + String.valueOf(list.size()));

		batchDeleteUsers.write(list);

	}

	/**
	 * 翻页查询用户
	 * 
	 * @author zhuwei
	 * @date 2013年8月19日 下午1:30:36
	 * @see com.zeny.express.module.mdm.user.service.UserService#queryUsersByPage(com.zeny.express.data.mdm.entity.UserEntity,
	 *      com.zeny.express.framework.core.domain.Page)
	 */
	@Override
	public Page<UserEntity> searchUsersByPage(UserSearchParameterDto condition,
			Page<UserEntity> page) {

		LOGGER.debug(condition.toString());
		LOGGER.debug(page.toString());

		MasterSlaveContext.setMaster("mdmdb");
		
		page.addAggregation("operation", "'合计：'");
		page.addAggregation("loginCode", "CONCAT('平均长度：',cast(avg(length(login_code)) as decimal(10, 3)))");

		List<UserEntity> list = this.userEntityDao.searchUsersByPage(condition,
				page);
		page.setRows(list);
		
		return page;
	}

}
